package com.youhui.weixin.dao;

import com.youhui.weixin.domain.Goods;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * Created by https://github.com/kuangcp on 17-9-26  下午5:09
 */
@Repository
@RepositoryRestResource(path = "goods")
public interface GoodsDao extends JpaRepository<Goods,Long>{

    @Query("select g from Goods g where g.link like 'https://uland.taobao.com/%'")
    Page<Goods> findListByLink(Pageable pageable);

    @Query("select g from Goods g where g.name = ?1")
    Goods findOneByName(String name);

    @Query("select g from Goods g where g.name like ?1")
    Page<Goods> findListByName(String name, Pageable pageable);

    /**
     * 按购买量获取商品.
     * @param pageable pageable
     * @return 商品集合
     */
    @Query("select g from Goods g order by g.alreadyBuy desc ")
    Page<Goods> getHotGoods(Pageable pageable);

    /**
     * 获取某类型的商品.
     * @param typeId 类型
     * @return
     */
    @Query("select g from Goods g where g.typeId = ?1 order by g.alreadyBuy desc ")
    Page<Goods> getTypeGoods(int typeId, Pageable pageable);

    /**
     * 查询.
     * @param query 查询条件
     * @return
     */
    @Query("select g from Goods g where g.name like ?1")
    Page<Goods> getSearchGoods(String query, Pageable pageable);

    @Query("select count(g) from Goods g where TO_DAYS(g.importTime) = TO_DAYS(CURRENT_TIME())")
    int getTedayImportCount();

    @Query("select count(g) from Goods g")
    long getGoodsCount();

}
